import random
import time
import pygame
from pygame.constants import *


# 飞机类
class HeroPlane(pygame.sprite.Sprite):
    def __init__(self, screen):
        # 精灵初始化
        pygame.sprite.Sprite.__init__(self)
        # 创建飞机
        self.player = pygame.image.load("./picture/hero1.png")
        # 获取矩形及坐标
        self.rect = self.player.get_rect()
        self.rect.topleft = [480 / 2 - 100 / 2, 600]
        # 速度
        self.speed = 15
        # 背景
        self.screen = screen
        # 子弹
        self.bullets = pygame.sprite.Group()

    def key_control(self):
        # 监听键盘事件
        key_pressed = pygame.key.get_pressed()

        if key_pressed[K_w] or key_pressed[K_UP]:
            self.rect.top -= self.speed
        if key_pressed[K_s] or key_pressed[K_DOWN]:
            self.rect.bottom += self.speed
        if key_pressed[K_a] or key_pressed[K_LEFT]:
            self.rect.left -= self.speed
        if key_pressed[K_d] or key_pressed[K_RIGHT]:
            self.rect.right += self.speed
        if key_pressed[K_SPACE]:
            # 发射子弹
            bullet = Bullet(self.screen, self.rect.left, self.rect.top)
            self.bullets.add(bullet)

    def display(self):
        # 飞机合成
        self.screen.blit(self.player, self.rect)
        # 更新子弹坐标
        self.bullets.update()
        # 添加子弹
        self.bullets.draw(self.screen)

    def update(self):
        self.key_control()
        self.display()


# 敌机类
class EnemyPlane(pygame.sprite.Sprite):
    def __init__(self, screen):
        # 精灵初始化
        pygame.sprite.Sprite.__init__(self)
        # 创建飞机
        self.player = pygame.image.load("./picture/enemy0.png")
        # 获取矩形及坐标
        self.rect = self.player.get_rect()
        self.rect.topleft = [0, 0]
        # 速度
        self.speed = 15
        # 记录当前的窗口对象
        self.screen = screen
        # 装子弹的列表
        self.bullets = pygame.sprite.Group()
        # 敌机移动方向
        self.direction = 'right'

    def display(self):
        # 飞机合成
        self.screen.blit(self.player, self.rect)
        # 更新子弹坐标
        self.bullets.update()
        # 添加子弹
        self.bullets.draw(self.screen)

    def auto_move(self):
        if self.direction == 'right':
            self.rect.right += self.speed
        elif self.direction == 'left':
            self.rect.right -= self.speed

        if self.rect.right > 480 + 51:
            self.direction = 'left'
        elif self.rect.right < 0:
            self.direction = 'right'

    def auto_fire(self):
        random_num = random.randint(1, 20)
        if random_num == 10:
            bullet = EnemyBullet(self.screen, self.rect.left, self.rect.right)
            self.bullets.add(bullet)

    def update(self):
        self.auto_move()
        self.display()
        self.auto_fire()


# 子弹类
class Bullet(pygame.sprite.Sprite):
    def __init__(self, screen, x, y):
        # 精灵类初始化
        pygame.sprite.Sprite.__init__(self)
        # 创建子弹
        self.image = pygame.image.load("./picture/bullet.png")
        # 获取矩形及坐标
        self.rect = self.image.get_rect()
        self.rect.topleft = [x + 100 / 2 - 22 / 2, y - 22]
        # 背景
        self.screen = screen
        # 速度
        self.speed = 15

    def update(self):
        # 子弹飞
        self.rect.top -= self.speed
        # 出界销毁
        if self.rect.top < -22:
            self.kill()


# 敌机子弹类
class EnemyBullet(pygame.sprite.Sprite):
    def __init__(self, screen, x, y):
        # 精灵类初始化
        pygame.sprite.Sprite.__init__(self)
        # 创建子弹
        self.image = pygame.image.load("./picture/bullet1.png")
        # 获取矩形及坐标
        self.rect = self.image.get_rect()
        self.rect.topleft = [x + 50 / 2 - 8 / 2, y + 39]
        # 背景
        self.screen = screen
        # 速度
        self.speed = 20

    def update(self):
        # 子弹飞
        self.rect.top += self.speed
        # 出界销毁
        if self.rect.top > 852:
            self.kill()


# 声音类
class GameSound(object):
    def __init__(self):
        pygame.mixer.init()  # 音乐模块初始化
        pygame.mixer.music.load('./picture/bg2.ogg')
        pygame.mixer.music.set_volume(0.5)  # 声音大小

    def playbackgroundmusic(self):
        pygame.mixer.music.play(-1)  # 开始播放音乐


def main():
    # 声音
    sound = GameSound()
    sound.playbackgroundmusic()
    # 创建游戏窗口
    screen = pygame.display.set_mode((480, 852), 0, 32)
    # 创建背景图片
    background = pygame.image.load("./picture/background.png")
    # 调用飞机类1
    player = HeroPlane(screen)
    # 调用敌机
    enemyplane = EnemyPlane(screen)

    while True:
        # 背景合成
        screen.blit(background, (0, 0))

        # 获取事件
        for event in pygame.event.get():
            # 判断事件类型
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

        player.update()
        enemyplane.update()

        # 显示内容
        pygame.display.update()
        time.sleep(0.01)


if __name__ == '__main__':
    main()
